package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.service.ISysConfigService;
import com.itheima.ydd.service.ISysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/sys_menu")
public class SysMenuController {

    @Autowired
    private ISysMenuService sysMenuService;


    //所有菜单
    @GetMapping("/allData")
    public ResultData allData() {

        List<SysMenuDto> sysMenuDtoList = new ArrayList<>();
        //拿到所有的菜单集合数据
        List<SysMenu> allMenuList = sysMenuService.list();
        LambdaQueryWrapper<SysMenu> lqw = new LambdaQueryWrapper<>();
        lqw.eq(SysMenu::getPid, 0);
        //拿到所有的父级菜单
        List<SysMenu> sysMenus = sysMenuService.list(lqw);

        //遍历
        for (SysMenu sysMenu : sysMenus) {
            //拿到所有父级的id
            Integer id = sysMenu.getId();
            //新建dto对象
            SysMenuDto sysMenuDto = new SysMenuDto();
            //拷贝父级菜单
            BeanUtils.copyProperties(sysMenu, sysMenuDto);

            //将父菜单添加进父菜单集合
            List<SysMenuDto> sysMenuDtos = getChild(id, allMenuList);
            //将递归封装的菜单添加为父菜单的子菜单
            sysMenuDto.setChildren(sysMenuDtos);
            sysMenuDtoList.add(sysMenuDto);
        }

        return ResultData.ok("", sysMenuDtoList);

    }

    private List<SysMenuDto> getChild(Integer id, List<SysMenu> allSysMenuList) {
        //创建一个dto集合用于封装菜单
        List<SysMenuDto> childList = new ArrayList<>();
        //遍历当前用户所拥有的所有权限
        for (SysMenu sysMenu : allSysMenuList) {
            //判断当前权限父id是否与传入父菜单id相同
            if (sysMenu.getPid().equals(id)) {
                //相同则创建dto对象拷贝
                SysMenuDto sysMenuDto = new SysMenuDto();
                BeanUtils.copyProperties(sysMenu, sysMenuDto);
                //添加进集合
                childList.add(sysMenuDto);
            }
        }
        for (SysMenuDto sysMenuDto : childList) {
            //遍历子菜单，再次调用方法本身进行递归
            List<SysMenuDto> child = getChild(sysMenuDto.getId(), allSysMenuList);
            sysMenuDto.setChildren(child);
        }
        //出递归条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }


    @PostMapping("/add")
    public ResultData add(@RequestBody SysMenu sysMenu) {

        sysMenuService.addMenu(sysMenu);

        return ResultData.ok("", null);

    }

    @GetMapping("del")
    public ResultData delete(@RequestParam Integer id) {
        sysMenuService.deleteMenu(id);
        return ResultData.ok("", null);
    }

    //数据回显
    @GetMapping("/rowInfo")
    public ResultData rowInfo(@RequestParam Integer id) {
        return sysMenuService.getInfo(id);
    }

    //修改
    @PostMapping("/update")
    public ResultData update(@RequestBody SysMenu sysMenu) {
        sysMenuService.updateMenu(sysMenu);
        return ResultData.ok("", null);

    }


}






































































































 /* List<SysMenuDto> sysMenuDtoList=new ArrayList<>();

        //创建条件构造器,根据父级目录pid进行查找,总共三级目录
        LambdaQueryWrapper<SysMenu> sysMenuLqw = new LambdaQueryWrapper<>();
        sysMenuLqw.eq(SysMenu::getPid, 0);
        //一级,pid=0
        List<SysMenu> sysMenus = sysMenuService.list(sysMenuLqw);

        for (SysMenu sysMenu : sysMenus) {
            Integer id = sysMenu.getId();
            LambdaQueryWrapper<SysMenu> sysMenuLqw2 = new LambdaQueryWrapper<>();
            sysMenuLqw2.eq(id != null, SysMenu::getPid, id);
            List<SysMenu> menus = sysMenuService.list(sysMenuLqw2);
            SysMenuDto sysMenuDto=new SysMenuDto();
            BeanUtils.copyProperties(sysMenu,sysMenuDto);
            sysMenuDtoList.add(sysMenuDto);
            //sysMenu.setChildren(menus);
            for (SysMenu menu : menus) {
                Integer pid = menu.getPid();
                LambdaQueryWrapper<SysMenu> sysMenuLqw3 = new LambdaQueryWrapper<>();
                sysMenuLqw3.eq(pid != null, SysMenu::getPid, pid);
                List<SysMenu> menuList = sysMenuService.list(sysMenuLqw3);

                BeanUtils.copyProperties(menu,sysMenuDto);
                sysMenuDtoList.add(sysMenuDto);
                //menu.setChildren(menuList);
            }

        }
*/

//  return ResultData.ok("", sysMenuDtoList);
        /*for (SysMenu sysMenu : sysMenus) {
            Integer id = sysMenu.getId();
            LambdaQueryWrapper<SysMenu> sysMenuLqw2 = new LambdaQueryWrapper<>();
            sysMenuLqw2.eq(id != null, SysMenu::getPid, id);
            List<SysMenu> menus = sysMenuService.list(sysMenuLqw2);

            SysMenuDto sysMenuDto = new SysMenuDto();
            BeanUtils.copyProperties(sysMenu, sysMenuDto);
            List<SysMenuDto> sysSonMenuDtoList = new ArrayList<>();
            for (SysMenu menu : menus) {
                Integer id2 = menu.getId();
                LambdaQueryWrapper<SysMenu> sysMenuLqw3 = new LambdaQueryWrapper<>();
                sysMenuLqw3.eq(id2 != null, SysMenu::getPid, id2);
                List<SysMenu> menuList = sysMenuService.list(sysMenuLqw3);

                BeanUtils.copyProperties(menu, sysMenuDto);
                sysSonMenuDtoList.add(sysMenuDto);
            }
            sysMenuDto.setChildren(sysSonMenuDtoList);

            sysMenuDtoList.add(sysMenuDto);
        }*/
